using GEM.Application.Common;
using GEM.Application.Dto.Permission;
using GEM.Application.Dto.Role;
using GEM.Application.Repositories;
using GEM.Domain.Entities.App;
using Microsoft.AspNetCore.Mvc;

namespace GEM.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class RoleController : ControllerBase
{
    private readonly IRoleService _roleService;
    //依赖注入
    public RoleController(IRoleService roleService)
    {
        _roleService = roleService;
    }

    [HttpPost]
    public async Task<IActionResult> CreateRole([FromBody] CreateRoleDto createRoleDto)
    {
        var isTrue = await _roleService.CreateRoleAsync(createRoleDto.RoleName, createRoleDto.DataScope);
        return Ok(isTrue);
    }

    [HttpPost("romoverole")]
    public async Task<IActionResult> RemoveRole([FromBody] RemoveRoleDto removeRoleDto)
    {
        var isTrue = await _roleService.RemoveRoleAsync(removeRoleDto.RoleId);
        return Ok(isTrue);
    }

    [HttpPost("assign")]
    public async Task<IActionResult> AssignPermission([FromBody] AssignPermissionDto assignPermissionDto)
    {
        var isTrue = await _roleService.AssignPermissionAsync(assignPermissionDto.RoleId, assignPermissionDto.PermissionId);
        return Ok(isTrue);
    }

    [HttpPost("removepermission")]
    public async Task<IActionResult> RemovePermission([FromBody] AssignPermissionDto removePermissionDto)
    {
        var isTrue = await _roleService.RemovePermissionAsync(removePermissionDto.RoleId, removePermissionDto.PermissionId);
        return Ok(isTrue);
    }

    [HttpGet("paged")]
    public async Task<IActionResult> GetUserPagedAsync(int pageIndex = 1, int pageSize = 10)
    {
        var result = await _roleService.GetRolePagedAsync(pageIndex, pageSize);
        return Ok(ApiResponse<PagedResult<RoleDto>>.Success(result));
    }

    [HttpPost("enable/{id}")]
    public async Task<IActionResult> EnableRoleAsync(Guid id)
    {
        var result = await _roleService.EnableRoleAsync(id);
        return Ok(result);
    }

    [HttpPost("disable/{id}")]
    public async Task<IActionResult> DisableRoleAsync(Guid id)
    {
        var result = await _roleService.DisableRoleAsync(id);
        return Ok(result);
    }


}